#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
        if(argc != 2){
                printf("./a.out mmap filename\n");
                return -1;
        }

        int fd = open(argv[1], O_RDWR);

        void* mem = mmap(NULL, 8, PROT_WRITE | PROT_READ,MAP_SHARED,fd,0);
        if(mem == MAP_FAILED){
                perror("mmap err");
                return -1;
        }
        close(fd);

        //拷贝数据
        memcpy(mem, "hello", sizeof("hello"));
        //strcpy(mem, "hello");
        //释放
        if(mem){
                munmap(mem, sizeof(mem));
        }

        return 0;
}
